package com.zx._01_基础._013_io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;

public class SystemDemo {

    public static void main(String[] args) throws Exception {
        fun6();
    }

    public static void fun1() throws Exception {
        String path = "C:\\Users\\zhangxue\\Desktop\\";

        String fileName = "新建文本文档.txt";

        // 只能读取一次数据,后面的两个方法读取不到数据
        BufferedReader br = new BufferedReader(new FileReader(path + File.separator + fileName));

        // 读取全部的文本内容
        Stream<String> lines = br.lines();
        lines.forEach(System.out::println);

        // 一行一行的读取文件
        String len = null;
        while ((len = br.readLine()) != null) {
            System.out.println(len);
        }

        // 使用数组读取
        char[] buffer = new char[1024];
        while (br.read(buffer) != -1) {
            System.out.println(new String(buffer));
        }

        br.close();
    }

    public static void fun2() throws Exception {
        String path = "C:\\Users\\zhangxue\\Desktop\\";

        String fileName = "新建文本文档.txt";

        FileInputStream fis = new FileInputStream(path + File.separator + fileName);

        // 使用数组读取
        byte[] buffer = new byte[1024];
        while (fis.read(buffer) != -1) {
            System.out.println(new String(buffer));
        }

        fis.close();
    }

    public static void fun3() throws Exception {
        String path = "C:\\Users\\zhangxue\\Desktop\\";

        String fileName = "新建文本文档.txt";

        Path paths = Paths.get(path, fileName);

        List<String> readAllLines = Files.readAllLines(paths);

        for (String s : readAllLines) {
            System.out.println(s);
        }
    }

    public static void fun4() throws Exception {
        String path = "C:\\Users\\zhangxue\\Desktop\\";

        String fileName = "新建文本文档.txt";

        Path paths = Paths.get(path, fileName);

        Scanner scanner = new Scanner(paths, StandardCharsets.UTF_8.name());

        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }

        scanner.close();
    }

    public static void fun5() throws Exception {
        String path = "C:\\Users\\zhangxue\\Desktop\\";

        String fileName = "新建文本文档.txt";

        String string = FileUtils.readFileToString(new File(path + File.separator + fileName), StandardCharsets.UTF_8);

        System.out.println(string);
    }

    public static void fun6() throws Exception {
        String path = "C:\\Users\\zhangxue\\Desktop\\";

        String fileName = "新建文本文档.txt";

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path + File.separator + fileName));
        // 单字节
        int by;
        while ((by = bis.read()) != -1) {
            System.out.print((char) by);
        }
        // 字节数组
        byte[] bys = new byte[1024];
        int len;
        while ((len = bis.read(bys)) != -1) {
            System.out.println(new String(bys, 0, len));
        }
        bis.close();
    }

}
